//
//  ViewController.m
//  ibeacon
//
//  Created by apple on 2016/12/16.
//  Copyright © 2016年 hzq. All rights reserved.
//

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CBCentralManagerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *lab;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (nonatomic, strong) CBCentralManager *manager;
@end

@implementation ViewController
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    
    switch (central.state) {
            
        case CBCentralManagerStatePoweredOn:
            
            NSLog(@"蓝牙已打开,请扫描外设");
            
            break;
            
        case CBCentralManagerStatePoweredOff:
            
            NSLog(@"蓝牙关闭...");
            
            break;
            
        default:
            
            break;
            
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; //重点这里要建立委托
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goIn) name:@"进入" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goOut) name:@"离开" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locat:) name:@"距离" object:nil];
}
-(void)locat:(NSNotification*)locat{
    CLBeacon* beacon = locat.object;
    self.lab.text = [NSString stringWithFormat:@"距离中心:%.2f米",beacon.accuracy];
    self.lab2.text = [NSString stringWithFormat:@"信号强度:%ld",beacon.rssi];
    
}
-(void)goIn{
    [self prompt:@"进入监测范围"];
}
-(void)goOut{
    [self prompt:@"离开监测范围"];
}
//提示框
-(void)prompt:(NSString *)message{
    // 1.创建alert控制器
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    // 2.添加按钮以及触发事件
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:action1];
    // 3.presentViewController弹出一个控制器
    [self presentViewController:alertVc animated:YES completion:^{
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
